﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using mscook.mvc.Filters;
using mscook.mvc.Models;
using mscook.core.services;
using mscook.mvc.Core;
using mscook.core.domain;
using System.Security.Principal;
using mscook.mvc.core;
using mscook.core.repositories;
namespace mscook.mvc.Controllers
{
    public class CookBookController : BaseController
    {

        private ILoginUserService iLoginUserService;
        private ICookBookService iCookBookService;

        public CookBookController(IActionBonusSettingService actionBonusSettingService, IResourceBonusSettingService resourceBonusSettingService, ILoginUserService loginUserService, ICookBookService iCookBookService)
            : base(actionBonusSettingService, resourceBonusSettingService, loginUserService)
        {
            this.iLoginUserService = loginUserService;
            this.iCookBookService = iCookBookService;
        }
        public ActionResult Index(string[] resourceTypes, string num = "", string keyword = "")
        {
            CookBookModel cookBookModel = new CookBookModel();
            cookBookModel.LoginUser = CurrentUser;
            long total = 0;
            PageBreakHelper pageBreakHelper = new PageBreakHelper();
            pageBreakHelper.Init(num, "12");


            if (resourceTypes != null)
            {
                IList<string> resoureTypeList = resourceTypes.Where(o => !string.IsNullOrEmpty(o)).ToList();
                //cookBookModel.CookBookList = iCookBookService.SelectPageList(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
                if (resoureTypeList.Count == 0)
                {
                    cookBookModel.CookBookList = GetByKeywordOrPage(pageBreakHelper, keyword, out total);
                }
                else
                {
                    cookBookModel.CookBookList = iCookBookService.SelectLimitbyIds(GetRsourceIdsByTypes(resoureTypeList, PurchasedCategory.CookBook), pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
                    cookBookModel.SelectResourceTypes = resoureTypeList;
                }

            }
            else
            {
                cookBookModel.CookBookList = GetByKeywordOrPage(pageBreakHelper,keyword,out total);
            }



            ViewData["Keyword"] = keyword;
            pageBreakHelper.SetViewDataKeys(ViewData, ViewBag, total);
            InitSourceType(cookBookModel, PurchasedCategory.CookBook);
            InitNavBar();
            return View(cookBookModel);
        }
        [ResourceBonusFilter]
        public ActionResult ReadCookBook(string id)
        {

            CookBook cookBook = iCookBookService.Get(id);
            if (cookBook != null)
            {
                ReadCookBookModel readCookBookModel = new ReadCookBookModel() {CookBook=cookBook };
                readCookBookModel.RelativeInfos = GetRelativeInfo(id).OrderBy(o => o.EntityIndex).ToList();
                readCookBookModel.LoginUser = CurrentUser;
                readCookBookModel.Magazine = GetPushMagazine();
                InitNavBar();
                ViewBag.RelativeInfoList = GetRelativeInfo(id).OrderBy(o => o.EntityIndex).ToList();
                return View(readCookBookModel);
            }

            return RedirectToAction("index");


           
        }

        private IList<CookBook> GetByKeywordOrPage(PageBreakHelper pageBreakHelper, string keyword, out long total)
        {
            if (string.IsNullOrEmpty(keyword))
            {
                return iCookBookService.SelectPageList(pageBreakHelper.Start, pageBreakHelper.PerValue, out total);
            }
            else
            {
                return iCookBookService.GetByKeyword(keyword, pageBreakHelper.Start, pageBreakHelper.PerValue, out total);

            }
        }

    }
}
